package top.go2do.auth.business.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.go2do.auth.business.user.module.*;
import top.go2do.auth.business.user.module.dto.RegisterDTO;
import top.go2do.auth.business.user.service.UserService;
import top.go2do.common.annotation.UserInfoFill;
import top.go2do.common.controller.TomatoController;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.module.UserPO;
import top.go2do.common.module.pageQuery.PageResult;

import java.util.List;

import javax.validation.Valid;

/**
 * @author xingyuji
 * @date 2025/3/9 14:06
 * @description
 */
@RequestMapping("/users")
@RestController
@Slf4j
public class UserController implements TomatoController<UserPO, UserCreateForm, UserModifyForm, Long, UserQueryCondition, UserQueryAllCondition, UserListVO> {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    void register(@Valid @RequestBody RegisterDTO registerDTO) {
        log.info("registerDTO:{}", registerDTO);
        /**
         * step1: 校验验证码是否正确
         */

        /**
         * step2: 检查用户名是否已存在
         */
        String username = registerDTO.getUsername();

        /**
         * step3: 密码（密文）解密后，检查设置的是否符合规则
         */
        String passwordHash = registerDTO.getPasswordHash();
        // TODO: 解密得到明文密码
        String passwordRaw = passwordHash;

        /**
         * step4: 创建用户
         */
        UserAddBO addBO = new UserAddBO();
        addBO.setAppId(registerDTO.getAppId());
        addBO.setUsername(username);
        String passwordSaveHash = userService.encryptRawPassword(passwordRaw);
        addBO.setPasswordHash(passwordSaveHash);
        userService.createUser(addBO);
    }

    @Override
    public UserPO create(UserCreateForm userCreateForm, UserInfoBO userInfo) {
        /**
         * step3: 密码（密文）解密后，检查设置的是否符合规则
         */
        String passwordHash = userCreateForm.getPasswordHash();
        // TODO: 解密得到明文密码
        String passwordRaw = passwordHash;
        String passwordSaveHash = userService.encryptRawPassword(passwordRaw);
        userCreateForm.setPasswordHash(passwordSaveHash);
        userCreateForm.setAppId(userInfo.getAppId());
        userCreateForm.setCreatedBy(userInfo.getUserId());
        return userService.create(userCreateForm);
    }

    @Override
    public void modifyById(Long id, UserModifyForm userModifyForm, UserInfoBO userInfo) {
        userModifyForm.setUserId(id);
        userModifyForm.setUpdatedBy(userModifyForm.getUserId());
        userService.modifyById(userModifyForm);
    }

    @Override
    public void removeById(Long id, UserInfoBO userInfo) {
        userService.removeById(id);
    }

    @Override
    public UserPO getById(Long id) {
        return userService.getById(id);
    }

    @Override
    public List<UserListVO> listAll(UserQueryAllCondition userQueryAllCondition) {
        return List.of();
    }

    @Override
    public PageResult<UserListVO> pageQuery(UserQueryCondition userQueryCondition) {
        PageResult<UserPO> pageResult = userService.pageQuery(userQueryCondition);
        List<UserListVO> volist = userService.po2vo(pageResult.getList());
        PageResult<UserListVO> finalResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, finalResult);
        finalResult.setList(volist);
        return finalResult;
    }

    @PatchMapping("/setRoles")
    public void setRoles(@Valid @RequestBody UserRolesForm userRolesForm, @UserInfoFill UserInfoBO userInfo) {
        userRolesForm.setUpdatedBy(userInfo.getUserId());
        userService.setRoles(userRolesForm);
    }
}
